package com.yanyuan.algorithm.search;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description 1.在set中读取第一个值
 * set:集合特征，无序，不可重复
 * @Author yanyuan
 * @Date 14:35 2020/5/6
 * @Version 1.0
 **/
public class SetDemo {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("周瑜");
        set.add("陆逊");
        set.add("黄盖");
        set.add("黄盖");
        set.add("孙坚");
        String first = searchFirst(set);
        System.out.println("first name: " + first);

        String[] names = {"周瑜", "郭嘉", "诸葛亮", "姜维"};
        String[] scores = {"周瑜,武力,82", "郭嘉,武力,80", "诸葛亮,武力81",
                "周瑜,谋略,98", "郭嘉,谋略,100", "诸葛亮,谋略,99"};

        //查找没有评分的将领
        Set<String> nonScoreNameSet = searchNonScore(names, scores);
        System.out.println("没有评分的将领: " + nonScoreNameSet);


    }

    /**
     * 查找没有评分的将领（求差集）
     * @param names
     * @param scores
     * @return
     */
    private static Set<String> searchNonScore(String[] names, String[] scores) {
        Set<String> nameSet = new HashSet<>();
        for (String val: names){
            nameSet.add(val);
        }

        Set<String> scoreSet = new HashSet<>();
        for (String val: scores){
            scoreSet.add(val.split(",")[0]);
        }
        nameSet.removeAll(scoreSet);
        return nameSet;
    }

    private static String searchFirst(Set<String> set){
        String first = null;
        //1. 使用迭代器
//        if (set.iterator() != null) {
//            first = set.iterator().next();
//            System.out.println("set first :  " + first);
//        }

        //2.使用List
        List<String> list = new ArrayList<>(set);
        first = list.get(0);
        return first;
    }
}
